package com.vcredit.creditcat.service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vcredit.creditcat.enumtype.MessageSendStatusEnum;
import com.vcredit.creditcat.enumtype.MessageTypeEnum;
import com.vcredit.creditcat.interceptor.SendMessageException;
import com.vcredit.creditcat.model.Message;
import com.vcredit.creditcat.model.Send;
import com.vcredit.creditcat.persistence.MessageCustomMapper;
import com.vcredit.creditcat.persistence.base.MessageMapper;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.framework.annotation.Log;

/**
 * 
 **/
@Service
public class MessageService {
	@Autowired
	private MessageMapper mapper;
	@Autowired
	private MessageCustomMapper messageCustomMapper;
	@Autowired
	private SendSmsService sendSmsService;

	public void insert(Message obj) {
		mapper.insert(obj);
	}

	public void update(Message obj) {
		mapper.update(obj);
	}

	public List<Message> findAll() {
		return mapper.findAll();
	}

	@SuppressWarnings("unused")
	private List<Message> find(Message obj) {
		return mapper.find(obj);
	}

	/**
	 * 分页查询未删除的消息
	 * 
	 * @param custId
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public List<Message> findByCustId(Long custId, Integer msgKind, Integer pageIndex, Integer pageSize) {
		Long cusid = custId;
		int msgkind = msgKind;
		int pageindex = pageIndex;
		int pagesize = pageSize;
		return messageCustomMapper.findByCustId(custId, msgKind, pageIndex * pageSize, (pageIndex + 1) * pageSize);
	}

	/**
	 * 获取用户最新通知
	 * @param custId
	 * @return
	 */
	public Long findLatestMessage(Long custId){
		return messageCustomMapper.findLatestMessage(custId);
	}

	/**
	 * 批量操作修改同一字段信息
	 * 
	 * @param msgIds
	 * @param msgMode
	 */
	public void modifyMessageByOperation(List<Long> msgIds, Message msgMode) {
		messageCustomMapper.batchUpdate(msgIds, msgMode);
	}

	/**
	 * 查询之前的数据
	 * @param msgMode
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Message> dataBeforeMsg(Message msgMode) {
		return messageCustomMapper.dataBeforeMsg(msgMode);
	}

	/**
	 * 查询未读消息
	 * 
	 * @param custId
	 * @return
	 */
	public List<Message> findUnreadByCustId(Long custId) {
		return messageCustomMapper.findUnreadByCustId(custId, null, null);
	}

	@Log
	public void sendVerificationCode(Send send, String tel, String code) throws Exception {
		try {
			String result = sendSmsService.sendSms(send.getRender(), send.getMessage(), Constants.SMS_CLTYPENAME,
					Constants.SMS_SIGNSTR_VCODE);
			if (!result.equals("true")) {
				throw new SendMessageException("发送失败");
			}
			messageResultExecute(null, MessageTypeEnum.VCODE.getCode(), null, null, send.getMessage(), null, null,
					send.getRender(), new Date(), null);
		} catch (InvocationTargetException e) {
			messageResultExecute(null, MessageTypeEnum.VCODE.getCode(), null, null, send.getMessage(), null, null,
					send.getRender(), new Date(), new Exception(e.getTargetException()));
		} catch (Exception e) {
			messageResultExecute(null, MessageTypeEnum.VCODE.getCode(), null, null, send.getMessage(), null, null,
					send.getRender(), new Date(), e);
		}

	}
	
	@Log
	public void sendNotice(Send send, String tel, String code) throws Exception {
		try {
			String result = sendSmsService.sendSms(send.getRender(), send.getMessage(), Constants.SMS_NEW_TYPENAME,
					Constants.SMS_NEW_SIGNSTR);
			if (!result.equals("true")) {
				throw new SendMessageException("发送失败");
			}
			messageResultExecute(null, MessageTypeEnum.SMS.getCode(), null, null, send.getMessage(), null, null,
					send.getRender(), new Date(), null);
		} catch (InvocationTargetException e) {
			messageResultExecute(null, MessageTypeEnum.SMS.getCode(), null, null, send.getMessage(), null, null,
					send.getRender(), new Date(), new Exception(e.getTargetException()));
		} catch (Exception e) {
			messageResultExecute(null, MessageTypeEnum.SMS.getCode(), null, null, send.getMessage(), null, null,
					send.getRender(), new Date(), e);
		}

	}


	private void messageResultExecute(Long msgId, Integer msgType, String msgTitle, String msgAbstract,
			String msgContent, Long sender, Long reciever, String senderTarget, Date sendTime, Exception e) {
		if (msgId != null) {
			if (e == null) {
				this.update(new Message(msgId, MessageSendStatusEnum.SEND.getCode(), null));
			} else {
				String errorMsg = e.getMessage().length() > 150 ? e.getMessage().substring(0, 150) : e.getMessage();
				this.update(new Message(msgId, MessageSendStatusEnum.NO_SEND.getCode(), errorMsg));
			}
		} else {
			if (e == null) {
				this.insert(new Message(msgType, msgTitle, msgContent, sender, reciever, senderTarget,
						MessageSendStatusEnum.SEND.getCode(), msgAbstract, null));
			} else {
				String errorMsg = e.getMessage().length() > 150 ? e.getMessage().substring(0, 150) : e.getMessage();
				this.insert(new Message(msgType, msgTitle, msgContent, sender, reciever, senderTarget,
						MessageSendStatusEnum.NO_SEND.getCode(), msgAbstract, errorMsg));
			}
		}
	}
}